package j2.shopmail;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import j2.shopmail.ui.dashboard.DashboardFragment;

public class OrderActivity extends AppCompatActivity
{
  public static ListView listView;
  public  static TextView content;
  public  static  OrderADP adp = new OrderADP();
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);
        OrderBaseInfo.getOrderList(MainActivity.user.name,this.<ProgressBar>findViewById(R.id.orderprocess));
        listView=findViewById(R.id.ordercheck);
        content=findViewById(R.id.ordercontent);
    }
}
class OrderADP extends BaseAdapter
{
    
    @Override
    public int getCount()
    {
        if(OrderBaseInfo.obj!=null)
        return OrderBaseInfo.obj.size();
        else
            return 0;
    }
    
    @Override
    public Object getItem(int position)
    {
        if(OrderBaseInfo.obj!=null)
            return OrderBaseInfo.obj.get(position);
        else
        return null;
    }
    
    @Override
    public long getItemId(int position)
    {
        return position;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View view= LayoutInflater.from(MainActivity.Self).inflate(R.layout.orderinfo, null);
        TextView textView=view.findViewById(R.id.orderinfo);
        final OrderBaseInfo baseInfo= OrderBaseInfo.obj.get(position);
        textView.setText("订单号："+baseInfo.OrderID+"\n状态："+(baseInfo.isFinsh==1?"完成":"未完成"));
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                AsycHttp<String,Order> http=new AsycHttp<>();
                http.setBackgroundAction(new AsycHttpAction<String, Order>() {
                    @Override
                    public Order BackGroundAction(String... params)
                    {
                       String c= ServerHelper.MakeGetOrderStr(baseInfo.OrderID);
                      JSON json = JSON.parseObject(HttpHelper.Get(c));
                    Order order =  json.toJavaObject(Order.class);
                        JSONObject object=(JSONObject) json;
                      JSONArray array=(JSONArray) object.get("items");
                      for(int i=0;i<array.size();i++)
                      {
                          order.getItem().add(array.getJSONObject(i).toJavaObject(OrderGood.class));
                      }
                        return order;
                    }
    
                    @Override
                    public void ProgressUpdateAction(View UI, Double... values)
                    {
        
                    }
    
                    @Override
                    public void ResultFunction(Order result)
                    {
                        String p="订单号:"+result.getOrderID()+"  "+"\n订单创建时间:"+result.getCreateTime()+"\n送货地址："+result.getAddress()+"\n联系电话："+result.getPhone()+"\n\n\n";
                        for(int i=0;i<result.getItem().size();i++)
                        {
                            OrderGood good = result.getItem().get(i);
                         Goods a=   DashboardFragment.Find(good.getGoodID());
                            p+="商品名："+a.Name+"  \n价格："+a.Price+"  \n个数："+good.getCount()+"个\n\n";
                        }
                        OrderActivity.content.setText(p);
                    }
    
                    @Override
                    public void PreDoFunction()
                    {
        
                    }
                });
                http.execute();
            }
        });
        return view;
    }
}